import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * 这是官方给的一些注释，可以配合 Sidebar侧边栏组件看看，对于一些细节配置很有用。
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

// 公共的路由 （其实就是个数组）
export const constantRoutes = [
  {
    path: '/',
    component: Layout,  // 这是一个框架组件，顶部和侧边栏会固定加载
    redirect: '/dashboard',  // 重定向的就是中间的内容部分
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },

  {
    path: '/redirect',
    component: Layout,
    hidden: true,   // 因为无需在侧边栏显示，可以用这个字段来控制隐藏 
    children: [
      {
        path: '/redirect/:path(.*)',   // (.*)可以匹配多层级路由，如果不写会匹配不上
        component: () => import('@/views/redirect/index')
      }
    ]
  },

  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  }
]


// 动态的路由 
export const asyncRoutes = [
  {
    path: '/user',
    component: Layout,
    redirect: '/user/create',
    meta: { title: "用户" },
    children: [
      {
        path: 'create',
        name: 'userCreate',
        component: () => import('@/views/user/create'),
        meta: { title: "用户新建页", icon: 'edit', roles: ['admin'] } // 只有管理员才能访问，不配置是默认显示
      },
      {
        path: 'list',
        name: 'userList',
        component: () => import('@/views/user/list'),
        meta: { title: "用户列表页", icon: 'list', roles: ['editor'] } // 管理员和编辑员可以访问
      },
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // 是否使用 history 模式
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置路由
}

export default router
